/*
 * Copyright 2007-2008, www.jsemantic.org, www.adolfoestevez.com,
 * http://semanticj2ee.blogspot.com/ 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jsemantic.servicerules.examples.energy;

import java.math.BigDecimal;
import java.util.Map;
import java.util.WeakHashMap;

import org.jsemantic.servicerules.examples.energy.services.InvoiceService;
import org.jservicerules.support.spring.SpringContextFactory;
import org.springframework.context.ApplicationContext;

/**
 * A factory for creating InvoiceService objects.
 */
public class InvoiceServiceFactory {

	/** The semantic services context. */
	private static String semanticServicesContext = "org/jsemantic/support/examples/energy/semantic-services.xml";

	/** The ctx. */
	private ApplicationContext ctx = null;

	/**
	 * Instantiates a new invoice service factory.
	 */
	public InvoiceServiceFactory() {
		ctx = SpringContextFactory
				.getClassPathApplicationContexFactory(semanticServicesContext);
	}

	/**
	 * Gets the single instance of InvoiceServiceFactory.
	 * 
	 * @return single instance of InvoiceServiceFactory
	 */
	public InvoiceService getInstance() {
		InvoiceService service = (InvoiceService) ctx.getBean("invoiceService");
		service.setSessionVariables(getSessionVariables());
		return service;
	}

	/**
	 * Gets the session variables.
	 * 
	 * @return the session variables
	 */
	private Map getSessionVariables() {
		Map variables = new WeakHashMap();
		variables.put("kwh", new BigDecimal(0.091939));
		variables.put("ppower", new BigDecimal(1.618345));
		variables.put("months", new Integer(2));
		return variables;
	}

}
